package com.qjz.diancan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qjz.diancan.mapper.CartMapper;
import com.qjz.diancan.mapper.OrderMapper;
import com.qjz.diancan.model.entity.Cart;
import com.qjz.diancan.model.entity.Order;
import com.qjz.diancan.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private CartMapper cartMapper;

    @Override
    @Transactional
    public void pay(Integer userId, List<Integer> ids)
    {
        // 生成订单号
        String orderNum = System.currentTimeMillis() + "";
        Order order = new Order();
        order.setOrderNum(orderNum);
        order.setUserId(userId);
        orderMapper.addOrder(order);
        cartMapper.deleteShopCarDataAll(ids, order.getId());
    }

    @Override
    public PageInfo<Order> findByPage(Integer pageNum, Integer pageSize, Integer userId)
    {
        PageHelper.startPage(pageNum, pageSize);
        List<Order> orders = orderMapper.findByPage(userId);
        orders.forEach(order -> {
            List<Cart> cartList = cartMapper.findByOrderId(order.getId());
            order.setCartList(cartList);
        });
        return new PageInfo<>(orders);
    }
}
